package com.jtlrm.ckd.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.CommonUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.BaseActivity;
import com.jtlrm.ckd.entity.HuanZheInfoEntity;
import com.jtlrm.ckd.entity.PageEntity;
import com.jtlrm.ckd.mvp.model.HuanZheGuanLiModel;
import com.jtlrm.ckd.mvp.view.adapter.HomeQueryHuanZheAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import ezy.ui.layout.LoadingLayout;

public class HomeQueryHuanZheActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back)
    ImageView backI;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.search_content)
    EditText searchContent;
    private int pageNum = 1;
    private int pageSize = 12;
    int type;
    HuanZheGuanLiModel huanZheGuanLiModel;
    HomeQueryHuanZheAdapter adapter;
    String keyWords = "";
    @BindView(R.id.loading)
    LoadingLayout loadingLayout;
    @BindView(R.id.clear_search)
    ImageView clearSearchI;

    public static void goSearch(Context context, int type) {
        Intent intent = new Intent(context, HomeQueryHuanZheActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_home_query_huan_zhe);
    }

    @Override
    protected void initView() {
        adapter = new HomeQueryHuanZheAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        huanZheGuanLiModel = new HuanZheGuanLiModel();
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        adapter.openLoadAnimation();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNum++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                loadData();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HuanZheInfoEntity entity = (HuanZheInfoEntity) adapter.getData().get(position);
                switch (type) {
                    case 3:
                        YingYangPingGuDetailActivity.goDetail(context, true, entity.getId(), null);
                        break;
                    case 4:
                        HomeLiangBiaoPingGuActivity.goDetail(context, entity.getId());
                        break;
                    case 5:
                        ShengHuaCheckDetailActivity.goDetail(context, true, entity.getId());
                        break;
                }
            }
        });
        searchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    pageNum = 1;
                    loadingLayout.showContent();
                    CommonUtil.hideSoftInput(context, searchContent);
                    smartRefreshLayout.autoRefresh();
                }
                return false;
            }
        });
        searchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                keyWords = s.toString();
            }
        });
        showEmpty();
        backI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        clearSearchI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setText(searchContent, "");
                smartRefreshLayout.autoRefresh();
            }
        });
    }

    private void loadData() {
        huanZheGuanLiModel.queryHuanZheList(pageNum, pageSize, keyWords, new CommonObserver<PageEntity<List<HuanZheInfoEntity>>>() {
            @Override
            public void onError(int errType, String errMessage) {
                closeFrshorLoadmore();
                showToast(errMessage);
                if (pageNum ==1){
                    showEmpty();
                }
            }

            @Override
            public void onResult(PageEntity<List<HuanZheInfoEntity>> data) {
                if (pageNum > 1) {
                    adapter.addData(data.getItems());
                } else {
                    adapter.replaceData(data.getItems());
                }
                if (pageSize > data.getItems().size()) {
                    smartRefreshLayout.setEnableLoadmore(false);
                } else {
                    smartRefreshLayout.setEnableLoadmore(true);
                }
                showEmpty();
                closeFrshorLoadmore();
                if (pageNum == 1 && data.getItems().size() == 0){
                    showToast("暂无此患者");
                }
            }
        }, lifecycleSubject);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }

    public void showEmpty() {
        if (adapter.getData() == null || adapter.getData().size() == 0) {
            loadingLayout.showEmpty();
        } else {
            loadingLayout.showContent();
        }
    }
}
